/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-12-12 15:45:14
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-12-13 15:59:43
 * @FilePath: \v3-shopping\client\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { title } from "process";
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

const rootRoutes: RouteRecordRaw[] = [
  {
    path: "home",
    name: "Home",
    meta: {
      cache: true,
      title: "首页",
    },
    component: () => import("../views/Home/Home.vue"),
  },
  {
    path: "ai",
    name: "Ai",
    meta: {
      cache: true,
      title: "AI穿搭",
    },
    component: () => import("../views/Ai/Ai.vue"),
  },
  {
    path: "message",
    name: "Message",
    meta: {
      cache: false,
      title: "消息",
    },
    component: () => import("../views/Message/Message.vue"),
  },
  {
    path: "shoppingcart",
    name: "ShoppingCart",
    meta: {
      cache: true,
      title: "购物车",
    },
    component: () => import("../views/ShoppingCart/ShoppingCart.vue"),
  },
  {
    path: "mine",
    name: "Mine",
    meta: {
      cache: true,
      title: "我的",
    },
    component: () => import("../views/Mine/Mine.vue"),
  },
];

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "App",
    component: () => import("../views/TheRoot.vue"),
    redirect: "/home",
    children: rootRoutes,
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/Register",
    name: "Register",
    component: () => import("../views/Register.vue"),
    meta: {
      title: "注册",
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 全局的路由守卫
const whitePath = ["/login", "/register", "/home", "/TheRoot"];
router.beforeEach((to: any, from: any, next: any) => {
  document.title = to.meta.title;
  if (!whitePath.includes(to.path)) {
    // 需要登录
    // 判断浏览器本地有无userInfo
    if (!localStorage.getItem("userInfo")) {
      router.push("/login");
      return;
    }
    next();
    return;
  }
  next();
  return;
});
export default router;
